const fs = require('fs');
const util = require('../util.js');

var data = fs.readFileSync('./datasets/housing.data.txt', 'utf8');

const transform = array => {
	for(var i = 0 ; i<array.length ; i++){
		if(array[i] == "" || typeof(array[i]) == "undefined" || array[i] == null){
			array.splice(i, 1);
			i= i-1;            
		}
	}
	return array
}

const loadTextTranForm = async (data, csvText) =>{
	await data.split('\n').forEach((d, i) => {
		csvText += transform(d.split(' ')).join(',') + '\n'
		// console.log(transform(d.split(' ')))
	})
	return csvText
}

const txtToCsv = async data => {
	var csvText = ''
	csvText = await loadTextTranForm(data, csvText)
	console.log(csvText)
	fs.writeFileSync('./datasets/housing.csv', csvText, 'utf8')
}

txtToCsv(data)